package com.wannengbang.cloudleader.dialog;

import android.app.Activity;
import android.app.Dialog;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;


import com.wannengbang.cloudleader.R;
import com.wannengbang.cloudleader.base.DataCallBack;
import com.wannengbang.cloudleader.bean.BaseResponseBean;
import com.wannengbang.cloudleader.bean.AgentListBean;
import com.wannengbang.cloudleader.bean.StoreListBean;
import com.wannengbang.cloudleader.homepage.model.HomePageModelImpl;
import com.wannengbang.cloudleader.utils.FastClickUtils;
import com.wannengbang.cloudleader.utils.ToastUtil;
import com.wannengbang.cloudleader.widget.ViewLoading;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 划拨或分配给门店
 */
public class GiveAgentDialog extends DialogFragment {
    @BindView(R.id.tv_commit)
    TextView tvCommit;
    @BindView(R.id.tv_agent_name)
    TextView tvAgentName;
    @BindView(R.id.edit_min_coding)
    EditText editMinCoding;
    @BindView(R.id.edit_max_coding)
    EditText editMaxCoding;
    @BindView(R.id.tv_current_num)
    TextView tvCurrentNum;
    @BindView(R.id.tv_select_name)
    TextView tvSelectName;
    @BindView(R.id.ll_select_agent)
    LinearLayout llSelectAgent;
    @BindView(R.id.tv_check_name)
    TextView tvCheckName;
    @BindView(R.id.view_check)
    View viewCheck;
    @BindView(R.id.ll_check)
    LinearLayout llCheck;
    @BindView(R.id.tv_continue_name)
    TextView tvContinueName;
    @BindView(R.id.view_continue)
    View viewContinue;
    @BindView(R.id.ll_continue)
    LinearLayout llContinue;
    @BindView(R.id.ll_code)
    LinearLayout llCode;
    private Dialog dialog;
    private OnCallBackListener onCallBackListener;
    private Activity mActivity;
    private String agent_id;
    private String store_no;
    private String type;
    private ArrayList<String> options1Items = new ArrayList<>();
    private List<AgentListBean.DataBean.ItemListBean> agentList;
    private List<StoreListBean.DataBean.ItemListBean> storeList;
    private String giveType;
    private String min_no;
    private String max_no;
    private int checkType = 1;
    private List<String> device_no_list;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        initWindow();
        View view = inflater.inflate(R.layout.dialog_give_agent, null);
        ButterKnife.bind(this, view);
        mActivity = getActivity();
        initView();
        return view;
    }

    public static GiveAgentDialog newInstance(String type, String giveType, List<String> device_no_list) {
        Bundle args = new Bundle();
        GiveAgentDialog fragment = new GiveAgentDialog();
        args.putString("type", type);
        args.putString("giveType", giveType);
        args.putSerializable("device_no_list", (Serializable) device_no_list);
        fragment.setArguments(args);
        return fragment;
    }

    public void setOnCallBackListener(OnCallBackListener onCallBackListener) {
        this.onCallBackListener = onCallBackListener;
    }

    //    初始化请求
    private void initView() {
        type = getArguments().getString("type");
        giveType = getArguments().getString("giveType");
        device_no_list = (List<String>) getArguments().getSerializable("device_no_list");
        if (device_no_list != null) {
            tvCurrentNum.setText(device_no_list.size() + "");
        } else {
            device_no_list = new ArrayList<>();
        }
        if (giveType.equals("1")) {
            tvSelectName.setText("选择合伙人");
            tvCheckName.setText("选中划拨");
            tvContinueName.setText("连号划拨");
        } else {
            tvSelectName.setText("选择门店");
            tvCheckName.setText("选中分配");
            tvContinueName.setText("连号分配");
        }

        editMinCoding.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                min_no = s.toString();
                if (!TextUtils.isEmpty(min_no) && !TextUtils.isEmpty(max_no)) {
                    long num = Long.parseLong(max_no) - Long.parseLong(min_no);
                    if (num >= 0) {
                        tvCurrentNum.setText((num + 1) + "");
                    }
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        editMaxCoding.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                max_no = s.toString();
                if (!TextUtils.isEmpty(min_no) && !TextUtils.isEmpty(max_no)) {
                    long num = Long.parseLong(max_no) - Long.parseLong(min_no);
                    if (num >= 0) {
                        tvCurrentNum.setText((num + 1) + "");
                    }
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

        llCode.setVisibility(View.GONE);
    }

    //设置window属性
    private void initWindow() {
        //去掉dialog的标题
        dialog = this.getDialog();
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setCanceledOnTouchOutside(true);
        dialog.setCancelable(true);
        Window window = dialog.getWindow();
        window.getDecorView().setPadding(0, 0, 0, 0);
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        lp.gravity = Gravity.BOTTOM;
        window.getAttributes().windowAnimations = R.style.commomDialog;
        window.setAttributes(lp);
        window.setBackgroundDrawable(new ColorDrawable());
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }

    @OnClick({R.id.ll_select_agent, R.id.tv_commit, R.id.ll_check, R.id.ll_continue})
    public void onViewClicked(View view) {
        if (FastClickUtils.isFastClick()) {
            return;
        }
        switch (view.getId()) {
            case R.id.ll_select_agent:
                ChooseAgentDialog dialog = ChooseAgentDialog.newInstance(type, giveType);
                dialog.show(getFragmentManager(), "dialog");
                dialog.setOnCallBackListener((name, chooseId) -> {
                    if (giveType.equals("1")) {
                        agent_id = chooseId;
                        tvAgentName.setText(name);
                    } else {
                        store_no = chooseId;
                        tvAgentName.setText(name);
                    }
                });
                break;
            case R.id.tv_commit:
                if (checkType == 1) {
                    String agentName = tvAgentName.getText().toString();
                    if (TextUtils.isEmpty(agentName)) {
                        if (giveType.equals("1")) {
                            ToastUtil.showShort("请选择合伙人");
                        } else {
                            ToastUtil.showShort("请选择门店");
                        }
                        return;
                    }
                    if (device_no_list.size() == 0) {
                        ToastUtil.showShort("请选择要划拨的机具");
                        return;
                    }
                    ViewLoading.showProgress(mActivity);
                    if (giveType.equals("1")) {
                        new HomePageModelImpl().requestSelectTransferAgent(agent_id, type, "", device_no_list, new DataCallBack<BaseResponseBean>() {
                            @Override
                            public void onSuccessful(BaseResponseBean bean) {
                                ToastUtil.showShort(bean.getMsg());
                                if (onCallBackListener != null) {
                                    onCallBackListener.onCallBack();
                                }
                                dismiss();
                            }

                            @Override
                            public void onFailed(String msg, String code) {

                            }
                        });
                    } else {
                        new HomePageModelImpl().requesSelectTransferStore(store_no, type, "", device_no_list, new DataCallBack<BaseResponseBean>() {
                            @Override
                            public void onSuccessful(BaseResponseBean bean) {
                                ToastUtil.showShort(bean.getMsg());
                                if (onCallBackListener != null) {
                                    onCallBackListener.onCallBack();
                                }
                                dismiss();
                            }

                            @Override
                            public void onFailed(String msg, String code) {

                            }
                        });
                    }
                }
                if (checkType == 2) {
                    String agentName = tvAgentName.getText().toString();
                    String minCoding = editMinCoding.getText().toString();
                    String maxCoding = editMaxCoding.getText().toString();

                    if (TextUtils.isEmpty(agentName)) {
                        if (giveType.equals("1")) {
                            ToastUtil.showShort("请选择合伙人");
                        } else {
                            ToastUtil.showShort("请选择门店");
                        }
                        return;
                    }
                    if (TextUtils.isEmpty(minCoding)) {
                        ToastUtil.showShort("请输入最小编码");
                        return;
                    }
                    if (TextUtils.isEmpty(maxCoding)) {
                        ToastUtil.showShort("请输入最大编码");
                        return;
                    }
                    ViewLoading.showProgress(mActivity);

                    if (giveType.equals("1")) {
                        new HomePageModelImpl().requestTransferAgent(agent_id, type, "", minCoding, maxCoding, new DataCallBack<BaseResponseBean>() {
                            @Override
                            public void onSuccessful(BaseResponseBean bean) {
                                ToastUtil.showShort(bean.getMsg());
                                if (onCallBackListener != null) {
                                    onCallBackListener.onCallBack();
                                }
                                dismiss();
                            }

                            @Override
                            public void onFailed(String msg, String code) {

                            }
                        });
                    } else {
                        new HomePageModelImpl().requesTransferStore(store_no, type, "", minCoding, maxCoding, new DataCallBack<BaseResponseBean>() {
                            @Override
                            public void onSuccessful(BaseResponseBean bean) {
                                ToastUtil.showShort(bean.getMsg());
                                if (onCallBackListener != null) {
                                    onCallBackListener.onCallBack();
                                }
                                dismiss();
                            }

                            @Override
                            public void onFailed(String msg, String code) {

                            }
                        });
                    }
                }

                break;
            case R.id.ll_check:
                checkType = 1;
                viewCheck.setVisibility(View.VISIBLE);
                viewContinue.setVisibility(View.INVISIBLE);
                tvCheckName.setTextColor(mActivity.getResources().getColor(R.color.theme_color));
                tvContinueName.setTextColor(mActivity.getResources().getColor(R.color.text_color_2));
                llCode.setVisibility(View.GONE);
                tvCurrentNum.setText(device_no_list.size() + "");
                break;
            case R.id.ll_continue:
                checkType = 2;
                viewCheck.setVisibility(View.INVISIBLE);
                viewContinue.setVisibility(View.VISIBLE);
                tvCheckName.setTextColor(mActivity.getResources().getColor(R.color.text_color_2));
                tvContinueName.setTextColor(mActivity.getResources().getColor(R.color.theme_color));
                llCode.setVisibility(View.VISIBLE);

                max_no = editMaxCoding.getText().toString();
                min_no = editMinCoding.getText().toString();
                if (!TextUtils.isEmpty(min_no) && !TextUtils.isEmpty(max_no)) {
                    long num = Long.parseLong(max_no) - Long.parseLong(min_no);
                    if (num >= 0) {
                        tvCurrentNum.setText((num + 1) + "");
                    }
                } else {
                    tvCurrentNum.setText("0");
                }
                break;
        }
    }

    public interface OnCallBackListener {
        void onCallBack();
    }
}
